# -*- coding:utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from .models import *


class ELAPIlogAdmin(admin.ModelAdmin):
    list_display = ["id", "time", "code", ]
    readonly_fields = ("time", "code", "request", "response",)

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def get_actions(self, request):
        actions = super(ELAPIlogAdmin, self).get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions


class ELFBlogAdmin(ELAPIlogAdmin):
    list_display = ["id", "regno", "time", "ftype", "code", "hits", ]
    list_display_links = ["regno", ]
    list_filter = ["ftype", ]
    search_fields = ("regno",)
    readonly_fields = ("regno", "time", "ftype", "code", "request", "response", "hits",)

    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(ELAPIlogAdmin, self).get_search_results(request, queryset, search_term)
        if search_term:
            from wlmodels.company.models import ELPost
            the_coms = ELPost.objects.filter(ENTNAME__istartswith=search_term).values_list("REGNO", flat=True)[:300]
            queryset = self.model.objects.filter(regno__in=list(the_coms))
        return queryset, use_distinct


class ELWmylogAdmin(ELAPIlogAdmin):
    list_display = ["id", "company_name", "time", "ftype", "code", "hits", ]
    list_display_links = ["company_name", ]
    list_filter = ["ftype", ]
    search_fields = ("^company_name",)
    readonly_fields = ("company_name", "time", "ftype", "code", "request", "response", "hits",)


# admin.site.register(ELFBlog, ELFBlogAdmin)
# admin.site.register(ELWmylog, ELWmylogAdmin)
